<?php

declare(strict_types=1);

namespace App\CustHub\Policies;

use App\CustHub\Helpers\ChLoginUser;
use Hyperf\Database\Model\Builder;
use Mine\MineModel;

class ModelPolicy
{
    protected $policies = [];

    public function __construct()
    {
        $this->policies = config('permissions.policies', []);
    }

    public function apply(Builder $builder, $model, ?int $uid = null)
    {
        $modelClass = get_class($model);
        if (isset($this->policies[$modelClass])) {
            $policyClass = $this->policies[$modelClass];
            $policy = new $policyClass();
            return $policy->apply($builder, $uid);
        }

        $defaultPolicy = new DefaultPolicy();
        $tableName = (new $modelClass)->getTable();
        return $defaultPolicy->apply($builder, $uid,$tableName);
    }
}